package com.blue.waffle.iwc.database;

import java.sql.SQLException;

import com.badlogic.gdx.utils.Disposable;
import com.blue.waffle.iwc.domains.helpers.DifficultyHelper;
import com.blue.waffle.iwc.domains.helpers.LevelHelper;
import com.blue.waffle.iwc.domains.helpers.ProfileHelper;
import com.blue.waffle.iwc.domains.helpers.ProfileLevelHelper;
import com.blue.waffle.iwc.domains.helpers.WordHelper;

public class SqliteDatabaseHelper implements Disposable {
	private SqliteActionResolver actionResolver;
	private ProfileHelper profileHelper;
	private WordHelper wordHelper;
	private DifficultyHelper difficultyHelper;
	private LevelHelper levelHelper;
	private ProfileLevelHelper profileLevelHelper;
	
	public SqliteDatabaseHelper(SqliteActionResolver actionResolver){
		this.actionResolver=actionResolver;
	}
	
	public ProfileHelper getProfileHelper() throws SQLException {
		if (profileHelper==null) {
			profileHelper=new ProfileHelper(actionResolver.getConnection());
		}else {
			if (profileHelper.isClosed()) {
				profileHelper.setConnection(actionResolver.getConnection());
			}
		}
		return profileHelper;
	}
	
	public WordHelper getWordHelper() throws SQLException {
		if (wordHelper==null) {
			wordHelper=new WordHelper(actionResolver.getConnection());
		}else {
			if (wordHelper.isClosed()) {
				wordHelper.setConnection(actionResolver.getConnection());
			}
		}
		return wordHelper;
	}
	
	public DifficultyHelper getDifficultyHelper() throws SQLException {
		if (difficultyHelper==null) {
			difficultyHelper=new DifficultyHelper(actionResolver.getConnection());
		}else {
			if (difficultyHelper.isClosed()) {
				difficultyHelper.setConnection(actionResolver.getConnection());
			}
		}
		return difficultyHelper;
	}
	
	public LevelHelper getLevelHelper() throws SQLException {
		if (levelHelper==null) {
			levelHelper=new LevelHelper(actionResolver.getConnection());
		}else {
			if (levelHelper.isClosed()) {
				levelHelper.setConnection(actionResolver.getConnection());
			}
		}
		return levelHelper;
	}
	
	public ProfileLevelHelper getProfileLevelHelper() throws SQLException {
		if (profileLevelHelper==null) {
			profileLevelHelper=new ProfileLevelHelper(actionResolver.getConnection());
		}else {
			if (profileLevelHelper.isClosed()) {
				profileLevelHelper.setConnection(actionResolver.getConnection());
			}
		}
		return profileLevelHelper;
	}

	@Override
	public void dispose() {
		try {
			if (profileHelper!=null) {
				profileHelper.close();
			}
			
			if (difficultyHelper!=null) {
				difficultyHelper.close();
			}
			
			if (levelHelper!=null) {
				levelHelper.close();
			}
			
			if (profileLevelHelper!=null) {
				profileLevelHelper.close();
			}
			
			if (wordHelper!=null) {
				wordHelper.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	

}
